package com.diyunapp.xilinapp.myindent;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.ReceiveBean;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddEvaluationListActivity extends StatusBarFragmentActivity {

    @Bind(R.id.integral_fresh_lv)
    ListView integralFreshLv;
    @Bind(R.id.integral_refresh_view)
    SwipeRefreshLayout integralRefreshView;
    public String gd_id;
    public String st_id;
    public String st_nm;
    public String re_id;
    public String gd_nm;
    public String gd_pi;
    public String gd_ig;
    public String content;
    public String or_id;
    public String or_st;
    public String or_am;
    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.title)
    TextView title;
    private String OrderId;
    private LayoutInflater inflater;
    private ReceiveAdapter receiveAdapter;
    private List<ReceiveBean.OrderGoodsBean> info = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_list);
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        OrderId = getIntent().getExtras().getString("OrderId");
        ButterKnife.bind(this);
        View headView;
        headView = findViewById(R.id.list_menubar_inc);
        super.MenuWidget(headView);
        title.setText("追加评价");
        receiveAdapter = new ReceiveAdapter(this);
        initFresh();
//        initView();
        integralFreshLv.setAdapter(receiveAdapter);
    }

    private void initFresh() {
        integralRefreshView.setColorSchemeColors(R.color.domain_orange, R.color.green,
                R.color.blue, R.color.yellow);

        integralRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.d("zhangxiaoxidebug", "myordersSwiperefreshlayout onRefresh...");
//                initView();
            }
        });

        integralRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.d("onRefresh", "invoke onRefresh...");
                if (MainActivity.myLogin.isLoginState()) {
                    initView();
                } else {
                    toast("您还没有登录！");
                    integralRefreshView.setRefreshing(false);
                }
            }
        });
    }
    private void initOldEvaluation() {
        String url="http://www.xilingou.com/mobile/index.php?act=goods&op=zjplinfo&order_id=933";
        HttpUtils http=new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");

                            if (result == 200) {
                                JSONArray data = new JSONArray("data");
                                for (int i = 0; i < data.length(); i++) {
                                    JSONObject recordObject = data.optJSONObject(i);
//                                    String
                                }


                            } else {
                                String data = resultObject.optString("data");
                                toast(data);
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();

                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {

                    }
                });
    }
    private void initView() {
        String url = ""+MainActivity.url+"act=goods&op=find_all_goods&order_id="
                + OrderId + "";
        Log.i("zhangxiaoxidebug","url"+url);
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
//                        bidFreshLv.onRefreshComplete();
                        integralRefreshView.setRefreshing(false);
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.getInt("code");
                            if (result == 200) {
                                info.clear();
                                JSONObject order_info = resultObject.optJSONObject("order_info");

                                JSONObject store_info = resultObject.optJSONObject("store_info");
                                String order_id=order_info.optString("order_id");
                                or_id=order_id;
                                String store_id = order_info.optString("store_id");
                                String store_name = order_info.optString("store_name");
                                String order_state=order_info.optString("order_state");
                                String order_amount=order_info.optString("order_amount");
                                or_am=order_amount;
                                or_st=order_state;
                                st_id = store_id;
                                st_nm = store_name;
                                JSONArray order_goods = resultObject.optJSONArray("order_goods");
                                for (int i = 0; i < order_goods.length(); i++) {
                                    JSONObject recordObject = order_goods.optJSONObject(i);

                                    String rec_id = recordObject.optString("rec_id");
                                    re_id = rec_id;
                                    String goods_id = recordObject.optString("goods_id");
                                    gd_id = goods_id;
                                    String goods_name = recordObject.optString("goods_name");
                                    gd_nm = goods_name;
                                    String goods_price = recordObject.optString("goods_price");
                                    gd_pi = goods_price;
                                    String goods_image = recordObject.optString("goods_image");
//                                    gd_ig = goods_image;
//                                    info.add(new ReceiveBean.OrderGoodsBean(
//                                            rec_id, goods_id, goods_name, goods_price, goods_image));
                                }

                                receiveAdapter.setList(info);
                                receiveAdapter.notifyDataSetChanged();

                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        integralRefreshView.setRefreshing(false);
                        toast("请求失败");
                    }
                });
    }

    @OnClick(R.id.go_back)
    public void onClick() {
        finish();
    }

    class ReceiveAdapter extends BaseAdapter {
        private List<ReceiveBean.OrderGoodsBean> info = new ArrayList<ReceiveBean.OrderGoodsBean>();
        private Context context;

        public ReceiveAdapter(Context context) {
            super();
            this.context = context;
        }

        public void setList(List<ReceiveBean.OrderGoodsBean> list) {
            this.info = list;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return null == info ? 0 : info.size();
        }

        @Override
        public Object getItem(int position) {
            return null == info ? null : info.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        private ViewHolder viewHolder;

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (null == convertView) {
                convertView = inflater.inflate(R.layout.item_receive_list, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            ReceiveBean.OrderGoodsBean bean = info.get(position);
            viewHolder.todo.setTag(position);
            viewHolder.introduceGoods.setText(bean.getGoods_name());
            viewHolder.introducePrice.setText(bean.getGoods_price());
            viewHolder.todo.setText("追加评价");
            //rec_id, goods_id, goods_name, goods_price, goods_image
            String image =bean.getGoods_image();
            Picasso.with(context)
                    .load(image)
                    .resize(200, 200)
                    .centerCrop()
                    .placeholder(R.drawable.list_default)
                    .error(R.drawable.list_default)
                    .into(viewHolder.goodsImage);
            viewHolder.todo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent1 = new Intent();
                    intent1.setClass(AddEvaluationListActivity.this, AddEvaluationActivity.class);

                    intent1.putExtra("order_id", or_id);

                    startActivity(intent1);
                }
            });
            return convertView;
        }

        class ViewHolder {
            @Bind(R.id.goods_image)
            ImageView goodsImage;
            @Bind(R.id.introduce_goods)
            TextView introduceGoods;
            @Bind(R.id.introduce_price)
            TextView introducePrice;
            @Bind(R.id.goods_color)
            TextView goodsColor;
            @Bind(R.id.goods_size)
            TextView goodsSize;
            @Bind(R.id.goods_num)
            TextView goodsNum;
            @Bind(R.id.todo)
            TextView todo;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }

}
